#!/usr/bin/php
<?php 

require_once "classes/IPBlock.php";

$testdefs = array(
    '10.10.20.10'   => true,
    '10.10.20.100'  => true,
    '10.10.20.1000' => false,
    '10.10.20.*'    => true,
    '10.10.*.*'     => false,
    '1.260.1.100'   => false,
    '198.168.1.10-11' => true,
    '127.0.0.1'     => true,
);


echo "IP check test -----------------------------\n";
foreach ($testdefs as $def => $expect) {
    $res = IPBlock::isLegalDefinition( $def );
    $status = ($res == $expect) ? 'OK' : 'FAIL!';
    echo "   $def ->-> $status\n";
}

echo "IP block test 1 -----------------------------\n";
$block = new IPBlock( 1, 'test', null, '192.168.1.10', "U R bl0CD", 0, 0, 1 );

$testdefs = array(
    '192.168.1.10' => 'BLOCKED',
    '192.168.1.1'  => 'OK',
    '192.145.3333.1' => 'FAIL',
    );

foreach ($testdefs as $def => $expect) {
    $res = $block->check_ip( $def );
    $status = ($res == $expect) ? 'OK' : 'FAIL!';
    echo "   $def to $res ->-> $status\n";
}


echo "IP block test 2 -----------------------------\n";
$block = new IPBlock( 1, 'test', null, '192.168.1.*', "U R bl0CD", 0, 0, 1 );

$testdefs = array(
    '192.168.1.10'   => 'BLOCKED',
    '192.168.1.1'    => 'BLOCKED',
    '192.168.10.1'   => 'OK',
    '192.145.3333.1' => 'FAIL',
    );

foreach ($testdefs as $def => $expect) {
    $res = $block->check_ip( $def );
    $status = ($res == $expect) ? 'OK' : 'FAIL!';
    echo "   $def to $res ->-> $status\n";
}


echo "IP block test 3 -----------------------------\n";
$block = new IPBlock( 1, 'test', null, '192.168.1.20-41', "U R bl0CD", 0, 0, 1 );

$testdefs = array(
    '192.168.1.2'   => 'OK',
    '192.168.1.20'   => 'BLOCKED',
    '192.168.1.33'    => 'BLOCKED',
    '192.168.1.41'    => 'BLOCKED',
    '192.168.1.42'    => 'OK',
    '192.168.10.1'   => 'OK',
    '192.145.3333.1' => 'FAIL',
    );

foreach ($testdefs as $def => $expect) {
    $res = $block->check_ip( $def );
    $status = ($res == $expect) ? 'OK' : 'FAIL!';
    echo "   $def to $res ->-> $status\n";
}

echo "IP block test 4 -----------------------------\n";
$block = new IPBlock( 1, 'test', null, '127.0.1.1', "U R bl0CD", 0, 0, 1 );

$testdefs = array(
    '127.0.1.1'      => 'BLOCKED',
    '192.168.1.1'    => 'OK',
    '192.168.10.1'   => 'OK',
    '192.145.3333.1' => 'FAIL',
    );

foreach ($testdefs as $def => $expect) {
    $res = $block->check_ip( $def );
    $status = ($res == $expect) ? 'OK' : 'FAIL!';
    echo "   $def to $res ->-> $status\n";
}



?>